﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;
using RockCompany.Master.Validator;

namespace RockCompany.Master.View
{
    public partial class SI04 : ParentFrm
    {
        #region Properties
        private Translator trans;
        private UserAuthenticate uAuth;
        private SI04FormModel frmModel;
        private string sUserName;
        private List<System.Reflection.PropertyInfo> product_DGV_Prop;
        private List<System.Reflection.PropertyInfo> mat_DGV_Prop;
        private int chooseType;
        private BindingSource bdSrc;
        #endregion

        #region Constructor
        public SI04()
        {
            InitializeComponent();
            this.trans = new Translator();
            loadDefaultData();
        }

        public SI04(Translator trans, string sUsername, Form caller)
            : base(sUsername, caller)
        {
            InitializeComponent();
            this.trans = trans;           
            sUserName = sUsername;
            Owner = caller;
            loadDefaultData();
        }
        #endregion

        #region Utility Methods
        private void loadDefaultData()
        {
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);


            frmModel = new SI04FormModel();
            frmModel.Owner = this;
            frmModel.translator = this.trans;
            trans.controlTranslate(this);

            comboBox_SearchType.DataSource = frmModel.matTypeLOV(-1);
            comboBox_SearchType.DisplayMember = NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU;
            comboBox_SearchType.ValueMember = SI04Const.S_TYPE_NAME;

            numericUpDown_SearchMonth.Value = DateTime.Today.Month;
            numericUpDown_SearchYear.Value = DateTime.Today.Year;

            product_DGV_Prop = new List<System.Reflection.PropertyInfo>();
            mat_DGV_Prop = new List<System.Reflection.PropertyInfo>();

            CONG_LENH_SX cl_temp = new CONG_LENH_SX();
            TON_KHO_DTO tk_teml = new TON_KHO_DTO();
            NGUYEN_VLIEU_VTU_DTO mat_temp = new NGUYEN_VLIEU_VTU_DTO();

            product_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_CHON));
            product_DGV_Prop.Add(cl_temp.GetType().GetProperty(CONG_LENH_SX.N_SO_LO));
            product_DGV_Prop.Add(cl_temp.GetType().GetProperty(TON_KHO_DTO.N_MA_MAU));
            product_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_DAU));
            product_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_NHAP));
            product_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_XUAT));
            product_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_CUOI));
            product_DGV_Prop.Add(cl_temp.GetType().GetProperty(CONG_LENH_SX.N_SL_TON_AN_TOAN));
            product_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_GHI_CHU));

            mat_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_CHON));
            mat_DGV_Prop.Add(mat_temp.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU));
            mat_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_DAU));
            mat_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_NHAP));
            mat_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_XUAT));
            mat_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_SL_CUOI));
            mat_DGV_Prop.Add(tk_teml.GetType().GetProperty(TON_KHO_DTO.N_GHI_CHU));

            frmModel.MatRes = frmModel.buildEmptyDataTable(mat_DGV_Prop);
            frmModel.ProdRes = frmModel.buildEmptyDataTable(product_DGV_Prop);

            frmModel.buildDataGrid(dataGridView_WHRes, mat_DGV_Prop, null);

            frmModel.buildDataGrid(dataGridView_ProdRes, product_DGV_Prop, null);

            frmModel.MatRes.Columns[TON_KHO_DTO.N_SL_CUOI].Expression = TON_KHO_DTO.N_SL_DAU + "+" + TON_KHO_DTO.N_SL_NHAP + "-" + TON_KHO_DTO.N_SL_XUAT;
            frmModel.ProdRes.Columns[TON_KHO_DTO.N_SL_CUOI].Expression = TON_KHO_DTO.N_SL_DAU + "+" + TON_KHO_DTO.N_SL_NHAP + "-" + TON_KHO_DTO.N_SL_XUAT;

            dataGridView_ProdRes.DataSource = frmModel.ProdRes;
            dataGridView_WHRes.DataSource = frmModel.MatRes;

            bdSrc = new BindingSource();
        }

        public void callChooser(int type)
        {
            button_CalWH.Enabled = false;
            button_SaveWH.Enabled = false;
            button_Choose.Enabled = true;
            chooseType = type;

            comboBox_SearchType.DataSource = frmModel.matTypeLOV(type);
            comboBox_SearchType.DisplayMember = NGUYEN_VLIEU_VTU_DTO.N_TEN_LOAI_NVL_VTU;
            comboBox_SearchType.ValueMember = SI04Const.S_TYPE_NAME;
        }

        public DataTable getSelectedMatsNo()
        {
            return frmModel.getSelectedMats(chooseType);
        }
        #endregion

        #region Event Methods
        private void button_SearchWH_Click(object sender, EventArgs e)
        {
            int year = int.Parse(numericUpDown_SearchYear.Value.ToString());
            int month = int.Parse(numericUpDown_SearchMonth.Value.ToString());

            string cfrmDlgCap = trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION);
            string calWHCfrm = trans.getMessageCaption(CommonConstantIF.CAL_WAREHOUSE);
            string prevWH = trans.getMessageCaption(CommonConstantIF.NULL_PREV_WAREHOUSE);

            int cbSelectedValue = Util.parseInteger(comboBox_SearchType.SelectedValue);

            dataGridView_WHRes.Visible = (cbSelectedValue == SI04Const.NO_MAT_TYPE);
            dataGridView_ProdRes.Visible = (cbSelectedValue == SI04Const.NO_PROD_TYPE);

            if (dataGridView_WHRes.Visible)
            {
                frmModel.MatRes = frmModel.seacrhWH(month, year, SI04Const.NO_MAT_TYPE, textBox_MatName.Text, comboBox_SearchType.Text);
                if (frmModel.MatRes.Rows.Count > 0)
                {
                    frmModel.MatRes.Columns[TON_KHO_DTO.N_SL_CUOI].Expression = TON_KHO_DTO.N_SL_DAU + "+" + TON_KHO_DTO.N_SL_NHAP + "-" + TON_KHO_DTO.N_SL_XUAT;
                    bdSrc.DataSource = frmModel.MatRes;
                    bdSrc.Filter = TON_KHO_DTO.N_SL_CUOI + " > 0";
                    dataGridView_WHRes.DataSource = bdSrc.DataSource;
                }
            }
            else
            {
                frmModel.ProdRes = frmModel.seacrhWH(month, year, SI04Const.NO_PROD_TYPE, textBox_MatName.Text, "");
                if (frmModel.ProdRes.Rows.Count > 0)
                {
                    frmModel.ProdRes.Columns[TON_KHO_DTO.N_SL_CUOI].Expression = TON_KHO_DTO.N_SL_DAU + "+" + TON_KHO_DTO.N_SL_NHAP + "-" + TON_KHO_DTO.N_SL_XUAT;
                    frmModel.ProdRes.DefaultView.RowFilter = TON_KHO_DTO.N_SL_CUOI + " > 0";
                    dataGridView_ProdRes.DataSource = frmModel.ProdRes.DefaultView;
                }
            }
        }

        private void button_SaveWH_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD)))
            {
                int year = int.Parse(numericUpDown_SearchYear.Value.ToString());
                int month = int.Parse(numericUpDown_SearchMonth.Value.ToString());

                bool bSuccess = false;

                if (dataGridView_WHRes.Visible)
                {
                    bSuccess = frmModel.saveData(month, year, SI04Const.NO_MAT_TYPE, comboBox_SearchType.Text);
                }
                else
                {
                    bSuccess = frmModel.saveData(month, year, SI04Const.NO_PROD_TYPE, "");  
                }
                if (bSuccess)
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                else
                    InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (dataGridView_ProdRes.Rows.Count > 0 || dataGridView_WHRes.Rows.Count > 0)
            {
                DataTable tempDB = null;
                if (dataGridView_WHRes.Visible)
                    tempDB = frmModel.MatRes;
                else
                    tempDB = frmModel.ProdRes;

                string wh_Time = numericUpDown_SearchMonth.Value.ToString() + "/" + numericUpDown_SearchYear.Value.ToString();
                if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_PRINT)))
                {

                    if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this), wh_Time, tempDB))
                    {
                        InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_CalWH_Click(object sender, EventArgs e)
        {
            int year = int.Parse(numericUpDown_SearchYear.Value.ToString());
            int month = int.Parse(numericUpDown_SearchMonth.Value.ToString());

            string cfrmDlgCap = trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION);
            string calWHCfrm = trans.getMessageCaption(CommonConstantIF.CAL_WAREHOUSE);
            string prevWH = trans.getMessageCaption(CommonConstantIF.NULL_PREV_WAREHOUSE);

            dataGridView_WHRes.Visible = (comboBox_SearchType.SelectedValue.Equals(SI04Const.NO_MAT_TYPE));
            dataGridView_ProdRes.Visible = (comboBox_SearchType.SelectedValue.Equals(SI04Const.NO_PROD_TYPE));

            if (dataGridView_WHRes.Visible)
            {

                frmModel.calWH(year, month, SI04Const.NO_MAT_TYPE, comboBox_SearchType.Text);

                button_SaveWH.Enabled = frmModel.MatRes.Rows.Count > 0;

                bdSrc.DataSource = frmModel.MatRes;
                bdSrc.Filter = TON_KHO_DTO.N_SL_CUOI + " > 0";
                dataGridView_WHRes.DataSource = bdSrc.DataSource;

                dataGridView_WHRes.Visible = true;
                dataGridView_WHRes.ReadOnly = false;
                dataGridView_ProdRes.Visible = false;
            }
            else
            {
                frmModel.calWH(year, month, SI04Const.NO_PROD_TYPE, "");

                button_SaveWH.Enabled = frmModel.ProdRes.Rows.Count > 0;

                bdSrc.DataSource = frmModel.ProdRes;
                bdSrc.Filter = TON_KHO_DTO.N_SL_CUOI + " > 0";
                dataGridView_ProdRes.DataSource = bdSrc.DataSource;

                dataGridView_WHRes.Visible = false;
                dataGridView_ProdRes.ReadOnly = false;
                dataGridView_ProdRes.Visible = true;
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView_ProdRes_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                double sl_cuoi = Util.parseDouble(dataGridView_ProdRes.Rows[e.RowIndex].Cells[TON_KHO_DTO.N_SL_CUOI].EditedFormattedValue.ToString());
                double sl_ton_an_toan = Util.parseDouble(dataGridView_ProdRes.Rows[e.RowIndex].Cells[CONG_LENH_SX.N_SL_TON_AN_TOAN].EditedFormattedValue.ToString());
                if (sl_cuoi >= sl_ton_an_toan || sl_cuoi < 0)
                {
                    dataGridView_ProdRes.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
        }

        private void dataGridView_WHRes_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int[] validColsIndex = {
                                       dataGridView_WHRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_DAU].ColumnIndex,
                                       dataGridView_WHRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_NHAP].ColumnIndex,
                                       dataGridView_WHRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_XUAT].ColumnIndex,
                                       dataGridView_WHRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_CUOI].ColumnIndex
                                   };
            if (validColsIndex.Contains(e.ColumnIndex))
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void dataGridView_ProdRes_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int[] validColsIndex = {
                                       dataGridView_ProdRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_DAU].ColumnIndex,
                                       dataGridView_ProdRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_NHAP].ColumnIndex,
                                       dataGridView_ProdRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_XUAT].ColumnIndex,
                                       dataGridView_ProdRes.CurrentRow.Cells[TON_KHO_DTO.N_SL_CUOI].ColumnIndex
                                   };
            if (validColsIndex.Contains(e.ColumnIndex))
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void comboBox_SearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //dataGridView_WHRes.Visible = (comboBox_SearchType.SelectedValue.Equals(SI04Const.NO_MAT_TYPE));
            //dataGridView_ProdRes.Visible = (comboBox_SearchType.SelectedValue.Equals(SI04Const.NO_PROD_TYPE));
        }

         private void dataGridView_WHRes_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                double sl_cuoi = Util.parseDouble(dataGridView_WHRes.Rows[e.RowIndex].Cells[TON_KHO_DTO.N_SL_CUOI].EditedFormattedValue.ToString());
                if (sl_cuoi < 0)
                {
                    dataGridView_WHRes.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
        }

         private void dataGridView_WHRes_CellContentClick(object sender, DataGridViewCellEventArgs e)
         {
             if (e.RowIndex > -1 && e.ColumnIndex == dataGridView_WHRes.Columns[TON_KHO_DTO.N_CHON].Index)
             {
                 string sNo = Util.parseString(dataGridView_WHRes.Rows[e.RowIndex].Cells[NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU].EditedFormattedValue);
                 frmModel.addRemoveCheckedList(e.ColumnIndex, frmModel.getCheckedRow(sNo, SI04Const.NO_MAT_TYPE));
             }
         }

         private void dataGridView_ProdRes_CellContentClick(object sender, DataGridViewCellEventArgs e)
         {
             if (e.RowIndex > -1 && e.ColumnIndex == dataGridView_ProdRes.Columns[TON_KHO_DTO.N_CHON].Index)
             {
                 string sNo = Util.parseString(dataGridView_ProdRes.Rows[e.RowIndex].Cells[CONG_LENH_SX.N_SO_LO].EditedFormattedValue);
                 frmModel.addRemoveCheckedList(dataGridView_ProdRes.Columns[TON_KHO_DTO.N_CHON].Index, frmModel.getCheckedRow(sNo, SI04Const.NO_PROD_TYPE));
             }
         }

         private void button_Choose_Click(object sender, EventArgs e)
         {
             this.Hide();
         }
        #endregion
    }
}
